#include "tool.h"
Tool* Tool::staticTool =new Tool();

Tool::Tool(QObject *parent)
    : QObject{parent}
{
    if(!(KIS.checkAndCreateInitializeSETUPDir()
          &&KIS.checkAndCreateKCFDirLocationDir())
        ){
        KIS.checkAndCreateFileToInitializeSettings();
    }
    KCFDirLocation=KIS.setupKCFDirLocation();
    // emit joctoqml();
    // qDebug()<<KCFDirLocation;

    // connect(clientToServer,&QTcpSocket::readyRead,this,[](){

    checkAndCreateUserFile();


    // });

}

Tool *Tool::getStaticTool()
{
    return staticTool;
}

QByteArray Tool::getKCFDirLocation() const
{
    return KCFDirLocation;
}

void Tool::setKCFDirLocation(const QByteArray &newKCFDirLocation)
{
    KCFDirLocation = newKCFDirLocation;
}

void Tool::KCFanalysis()
{
    QList<QJsonObject> jsonObjToQMLList= KCFJH.analysis(KCFDirLocation);
    // QJsonDocument jsonDoc(jsonObjToQML);
    // QString jsonString=jsonDoc.toJson(QJsonDocument::Compact);
    for(QJsonObject jsonObjToQML:jsonObjToQMLList){
        // qDebug()<<jsonObjToQML;
        emit joctoqml(jsonObjToQML);


    }


    // qDebug()<<jsonObjToQML;
}

void Tool::handleQVariantToFile(QVariant variant)
{
    // qDebug()<<"123";
    QJsonObject jsonObj=QVariantMapToQJsonObject(variant.toMap());
    KIS.checkAndCreateKCFDirLocationDir();
    // qDebug()<<jsonObj;
    // qDebug()<<KCFDirLocation+"/"+jsonObj["name"].toString()+".kcf";
    QFile eachKCF(KCFDirLocation+"/"+jsonObj["id"].toString()+".kcf");
    if(!eachKCF.open(QIODevice::WriteOnly)){
        qDebug()<<"储存失败";
        return;
    }
    QJsonDocument jsonDoc(jsonObj);
    QString jsonObjString=jsonDoc.toJson();
    eachKCF.write(jsonObjString.toUtf8());
    eachKCF.close();
}

QJsonObject Tool::QVariantMapToQJsonObject(QVariantMap qvariantmap)
{
    // qDebug()<<qvariantmap.value("text");
    // qDebug()<<qvariantmap.toMap()["name"];
    // qDebug()<<qvariantmap;
    return QJsonObject::fromVariantMap(qvariantmap);
}

void Tool::toolWriteToLogin(QByteArray acc, QByteArray psw,bool isSaveToLocation)
{
    // qDebug()<<acc;
    tmpAcc=acc;
    tmpPsw=psw;
    clientToServer->wirteToLogin(acc,psw,isSaveToLocation);
}

void Tool::connectToServer()
{
    clientToServer=new KTcpConnection(this,ip,port);
}

void Tool::setIsVip(const QString &newIsVip)
{
    if (isVip == newIsVip)
        return;
    isVip = newIsVip;
    emit isVipChanged();
}

void Tool::resetIsVip()
{
    setIsVip({}); // TODO: Adapt to use your actual default value
}

bool Tool::getIsLogined() const
{
    return isLogined;
}

void Tool::setIsLogined(bool newIsLogined)
{
    if (isLogined == newIsLogined)
        return;
    isLogined = newIsLogined;
    emit isLoginedChanged();
}

void Tool::resetIsLogined()
{
    setIsLogined({}); // TODO: Adapt to use your actual default value
}

bool Tool::getIsToSaveToLocation() const
{
    return isToSaveToLocation;
}

void Tool::setIsToSaveToLocation(bool newIsToSaveToLocation)
{
    isToSaveToLocation = newIsToSaveToLocation;
}

void Tool::createAutoLoginFile(QString acc,QString psw)
{
    QDir dir(this->userDIr);
    QFileInfoList fileInfoList=dir.entryInfoList(QDir::Files);
    if(fileInfoList.size()!=0){
        for(QFileInfo i: fileInfoList){
            QFile file(i.absoluteFilePath());
            file.remove();
        }

        QFile file(this->userDIr+"/"+ acc+".txt");
        // QByteArray acc=fileInfoList[0].baseName().toUtf8();
        // qDebug()<<fileInfoList[0].baseName();
        // qDebug()<<file.open(QIODevice::ReadOnly);
        if(file.open(QIODevice::WriteOnly)){
            file.write(psw.toUtf8());
            // qDebug()<<file.readAll();
            // QByteArray psw=file.readAll();
            // clientToServer->wirteToLogin(acc,psw,true);


        }
        file.close();
    }


}

QString Tool::getTmpAcc() const
{
    return tmpAcc;
}

void Tool::setTmpAcc(const QString &newTmpAcc)
{
    tmpAcc = newTmpAcc;
}

QString Tool::getTmpPsw() const
{
    return tmpPsw;
}

void Tool::setTmpPsw(const QString &newTmpPsw)
{
    tmpPsw = newTmpPsw;
}

void Tool::readFileOpenInfo(QByteArray file)
{
    qDebug()<<file;
    QFile fileToOpen(file);
    if(!fileToOpen.open(QIODevice::ReadOnly))
        emit this->sendFileOpenInfo("打开文件失败，文件可能不存在","");
    QByteArray content=fileToOpen.readAll();
    emit this->sendFileOpenInfo(content,file);
    fileToOpen.close();

}

void Tool::writeFileOpenInfo(QByteArray file)
{
    QFile fileToOpen(file);
    if(!fileToOpen.open(QIODevice::WriteOnly))
        qDebug()<<"打开文件失败";
    // emit this->sendFileOpenInfo("打开文件失败，文件可能不存在");
    // QByteArray content=fileToOpen.readAll();
    // emit this->sendFileOpenInfo(content);
    fileToOpen.write(file);
    fileToOpen.close();
}

// void Tool::readFileOpenInfo(QByteArray targetFile)
// {
//     qDebug()<<targetFile;


// }

QString Tool::getVip() const
{
    return vip;
}

void Tool::setVip(const QString &newVip)
{
    if (vip == newVip)
        return;
    vip = newVip;
    emit vipChanged();
}

void Tool::resetVip()
{
    setVip({}); // TODO: Adapt to use your actual default value
}

QString Tool::getId() const
{
    return id;
}

void Tool::setId(const QString &newId)
{
    if (id == newId)
        return;
    id = newId;
    emit idChanged();
}

void Tool::resetId()
{
    setId({}); // TODO: Adapt to use your actual default value
}

QString Tool::getName() const
{
    return name;
}

void Tool::setName(const QString &newName)
{
    if (name == newName)
        return;
    name = newName;
    emit nameChanged();
}

void Tool::resetName()
{
    setName({}); // TODO: Adapt to use your actual default value
}

void Tool::checkAndCreateUserFile()
{
    QDir dir(this->userDIr);
    if(!dir.exists())
    {
        dir.cdUp();
        dir.mkdir(this->userDIr);
        // dir.cd(this->userDIr);
    }
}

void Tool::checkToLoginAuto()
{
    QDir dir(this->userDIr);
    QFileInfoList fileInfoList=dir.entryInfoList(QDir::Files);
    if(fileInfoList.size()!=0){
        QFile file(this->userDIr+"/"+ fileInfoList[0].fileName());
        QByteArray acc=fileInfoList[0].baseName().toUtf8();
        // qDebug()<<fileInfoList[0].baseName();
        // qDebug()<<file.open(QIODevice::ReadOnly);
        if(file.open(QIODevice::ReadOnly)){
            // qDebug()<<file.readAll();
            QByteArray psw=file.readAll();
            this->toolWriteToLogin(acc,psw,true);
            // clientToServer->wirteToLogin(acc,psw,true);
        }
        file.close();
    }
}

QString Tool::getIsVip() const
{
    return isVip;
}




